package com.xjrsoft.module.erpModel.caseErpSupplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.erpModel.caseErpMaterial.entity.CaseErpMaterial;
import com.xjrsoft.module.erpModel.caseErpMaterial.mapper.CaseErpMaterialMapper;
import com.xjrsoft.module.erpModel.caseErpSupplier.dto.AddCaseErpSupplierMaterialDto;
import com.xjrsoft.module.erpModel.caseErpSupplier.entity.CaseErpSupplyMaterial;
import com.xjrsoft.module.erpModel.caseErpSupplier.mapper.CaseErpSupplyMaterialMapper;
import com.xjrsoft.module.erpModel.caseErpSupplier.service.ICaseErpSupplyMaterialService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 供应商供货清单【case_erp_supply_material】 服务实现类
 * </p>
 *
 * @author hnyyzy
 * @since 2023-07-05
 */
@Service
@AllArgsConstructor
public class CaseErpSupplyMaterialServiceImpl extends MPJBaseServiceImpl<CaseErpSupplyMaterialMapper, CaseErpSupplyMaterial> implements ICaseErpSupplyMaterialService {

    private final CaseErpMaterialMapper caseErpMaterialMapper;
    @Override
    public Boolean add(AddCaseErpSupplierMaterialDto dto) {
        if (ObjectUtil.isNotEmpty(dto.getCode())){
            List<CaseErpMaterial> caseErpMaterialList = caseErpMaterialMapper.selectList(Wrappers.<CaseErpMaterial>query().lambda().eq(CaseErpMaterial::getCode, dto.getCode()));
            if(CollectionUtil.isNotEmpty(caseErpMaterialList)){
                CaseErpMaterial caseErpMaterial = caseErpMaterialList.get(0);
                if (StrUtil.isNotBlank(caseErpMaterial.getModel())){
                    dto.setModel(caseErpMaterial.getModel());
                }
            }
        }
        CaseErpSupplyMaterial caseErpSupplyMaterial = BeanUtil.toBean(dto, CaseErpSupplyMaterial.class);
        this.save(caseErpSupplyMaterial);
        return true;
    }
}
